import { FormItemProps, UploadFile } from 'antd';
import { YmUpload as Upload } from './YmUpload';

interface YmUploadUtils {
  formRules: (
    requiredMsg?: string | false,
  ) => NonNullable<FormItemProps['rules']>;
}

const YmUpload: typeof Upload & YmUploadUtils = Upload as any;

YmUpload.formRules = (requiredMsg) => {
  const res: NonNullable<FormItemProps['rules']> = [
    {
      async validator(rule: any, value: UploadFile[]) {
        if (value) {
          if (value.some((v) => v.status === 'error')) {
            throw new Error('有失败的图片');
          } else if (value.some((v) => v.status === 'uploading')) {
            throw new Error('有正在上传的文件');
          }
        }
      },
    },
  ];
  if (requiredMsg) {
    res.push({
      required: true,
      message: requiredMsg,
    });
  }

  return res;
};

export type { YmUploadProps } from './YmUpload';
export { YmUpload };
